package com.woniuxy.controller;


import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.entity.AddQuestionEntity;
import com.woniuxy.entity.QuestionOption;
import com.woniuxy.entity.QuestionTopic;
import com.woniuxy.entity.ResponseEntity;
import com.woniuxy.service.QuestionService;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;


import java.util.*;


@RestController
@RequestMapping("/question")
@CrossOrigin
public class QuestionController {
    @Autowired
    QuestionService questionService;


    @RequestMapping ("/addQ")
    public ResponseEntity addQuestion(@RequestBody AddQuestionEntity addQuestionEntity){
        if(addQuestionEntity.getDaan() instanceof ArrayList){
            System.out.println("是数组");
        }else{
            System.out.println("不是数组");
        }


        return questionService.addQuestionDan(addQuestionEntity);
    }
    @RequestMapping ("/addO")
    public ResponseEntity addOption(@RequestBody QuestionOption questionOption){



        return questionService.addOption(questionOption);
    }

    @RequestMapping("/findSub")
    public String[] findSub(){
        return questionService.findSub();
    }
    @RequestMapping("/findGrade")
    public String[] findGrade(){
        return questionService.findGrade();
    }


    //查找题目列表、模糊搜索
    @RequestMapping("/findallQ/{currentPage}/{pageSize}")
    public ResponseEntity findAllQ(@PathVariable int currentPage,@PathVariable int pageSize, @RequestBody String jsonString){
        //初始化hashmap
        Map<String, Object> map = new HashMap<>();
        //将json字符串转换为json对象
        JSONObject json = JSONObject.parseObject(jsonString);
        //将json对象循环转换为map
        Iterator it = json.entrySet().iterator();
        while (it.hasNext()){
            Map.Entry<String, Object> entry = (Map.Entry<String, Object>) it.next();
            if (entry.getValue().toString().length() == 0 || entry.getValue().toString().equals("[]")){
                map.put(entry.getKey(), null);
            }else {
                map.put(entry.getKey(), entry.getValue());
                //利用工具类将数组的中括号去掉
                if(entry.getKey().toString().equals("ids")){
                  map.put(entry.getKey(), StringUtils.strip(entry.getValue().toString(),"[]"));
                }
            }
        }
        PageHelper.startPage(currentPage,pageSize);
        List<QuestionTopic> all = questionService.findAllQ(map);
        //添加分页信息
        PageInfo<QuestionTopic> questionTopicPageInfo = new PageInfo<>(all);
        return new ResponseEntity(questionTopicPageInfo);
    }

    @DeleteMapping("/deleteQbyid/{id}")
    public ResponseEntity deleteQbyId(@PathVariable int id){
        //System.out.println(id);
        return questionService.deleteQbyId(id);
    }

    @RequestMapping("/findQbyid/{id}")
    public ResponseEntity findQbyid(@PathVariable int id){
        List<QuestionTopic> oneQ = questionService.findQbyid(id);
        return new ResponseEntity(oneQ);
    }

    @RequestMapping("/findQubyid/{id}")
    public ResponseEntity findQubyid(@PathVariable int id){
        List<QuestionTopic> oneQ = questionService.findQubyid(id);
        return new ResponseEntity(oneQ);
    }

}
